package com.swt.shoppingmall.shoppingcart;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.swt.shoppingmall.R;
import com.swt.shoppingmall.base.BaseFragment;
import com.swt.shoppingmall.home.bean.GoodsBean;
import com.swt.shoppingmall.shoppingcart.adapter.ShoppingCartAdapter;
import com.swt.shoppingmall.shoppingcart.utils.CartStorage;
import com.swt.shoppingmall.view.NumberAddSubView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 购物车
 * Created by swt on 2016/12/23.
 */

public class ShoppingCartFragment extends BaseFragment {
    @Bind(R.id.ib_good_info_back)
    ImageButton ibGoodInfoBack;
    @Bind(R.id.ib_good_info_more)
    ImageButton ibGoodInfoMore;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.recyclerview)
    RecyclerView recyclerview;
    @Bind(R.id.checkbox_all)
    CheckBox checkboxAll;
    @Bind(R.id.tv_shopcart_total)
    TextView tvShopcartTotal;
    @Bind(R.id.btn_check_out)
    Button btnCheckOut;
    @Bind(R.id.ll_check_all)
    LinearLayout llCheckAll;
    @Bind(R.id.cb_all)
    CheckBox cbAll;
    @Bind(R.id.btn_delete)
    Button btnDelete;
    @Bind(R.id.btn_collection)
    Button btnCollection;
    @Bind(R.id.ll_delete)
    LinearLayout llDelete;
    @Bind(R.id.iv_empty)
    TextView ivEmpty;
    @Bind(R.id.tv_empty_cart_tobuy)
    TextView tvEmptyCartTobuy;
    @Bind(R.id.ll_empty_shopcart)
    LinearLayout llEmptyShopcart;
    private ShoppingCartAdapter adapter;

    //编辑状态
    private static final int ACTION_EDIT = 1;
    //完成状态
    private static final int ACTION_COMPLETE = 2;

    @Override
    public View initView() {
        View view = View.inflate(mActivity, R.layout.shopping_fragment, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void initData() {
        super.initData();
        tvTitle.setText(getString(R.string.gwc));
        ibGoodInfoBack.setVisibility(View.GONE);
        ibGoodInfoMore.setVisibility(View.GONE);
        tvRight.setTag(ACTION_EDIT);
        showData();

    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void showData() {
        List<GoodsBean> goodsBeanList = CartStorage.getInstance().getAllData();
        if (null != goodsBeanList && goodsBeanList.size() > 0) {

            tvRight.setVisibility(View.VISIBLE);
            tvRight.setText(getString(R.string.bj));
            //有数据，隐藏空的View，设置适配器
            llCheckAll.setVisibility(View.VISIBLE);
            llEmptyShopcart.setVisibility(View.GONE);
            adapter = new ShoppingCartAdapter(getActivity(), goodsBeanList, tvShopcartTotal, checkboxAll, cbAll);
            recyclerview.setAdapter(adapter);
            //设置布局管理器
            recyclerview.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false));
        } else {
            //没有数据
            //显示数据为空的数据
            emptyShoppingCart();
        }
    }

    //显示数据为空的数据
    private void emptyShoppingCart() {
        llEmptyShopcart.setVisibility(View.VISIBLE);
        tvRight.setVisibility(View.GONE);
        llDelete.setVisibility(View.GONE);
    }

    @OnClick({R.id.tv_right, R.id.checkbox_all, R.id.btn_check_out, R.id.ll_check_all, R.id.cb_all, R.id.btn_delete, R.id.btn_collection})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_right:
                int action = (int) view.getTag();
                if (action == ACTION_EDIT) {
                    //切换到完成状态
                    showDelete();
                } else {
                    hideDelete();
                }
                break;
            case R.id.checkbox_all:
                break;
            case R.id.btn_check_out:
                break;
            case R.id.ll_check_all:
                break;
            case R.id.cb_all:
                break;
            case R.id.btn_delete:
                //删除选中
                adapter.deleteData();
                //校验状态
                adapter.checkAll();

                //数据大小为0
                if (adapter.getItemCount() == 0) {
                    emptyShoppingCart();
                }
                break;
            case R.id.btn_collection:
                break;
        }
    }

    private void hideDelete() {
        //设置状态和文本
        tvRight.setTag(ACTION_EDIT);
        tvRight.setText(R.string.bj);
        //变为非勾选
        if (adapter != null) {
            adapter.checkAll_None(true);
            adapter.checkAll();
            adapter.showTotalPrice();
        }
        //删除视图显示
        llDelete.setVisibility(View.GONE);
        //结算视图隐藏
        llCheckAll.setVisibility(View.VISIBLE);
    }

    private void showDelete() {
        //设置状态和文本
        tvRight.setTag(ACTION_COMPLETE);
        tvRight.setText(R.string.wc);
        //变为非勾选
        if (adapter != null) {
            adapter.checkAll_None(false);
            adapter.checkAll();
        }
        //删除视图显示
        llDelete.setVisibility(View.VISIBLE);
        //结算视图隐藏
        llCheckAll.setVisibility(View.GONE);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
